package factory;

import anno.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/11/29  8:41
 */
public class Test {
    public static void main(String[] args) throws Exception {
        //获取类上面的注解
        //1.获取Class对象
        Class clz=Class.forName("factory.Apple");
        if (clz.isAnnotationPresent(Component.class)){ //判断类上门有没有写Component注解
            //获取注解对象
            Component component=(Component)clz.getAnnotation(Component.class);
            //调用注解对象里面方法
            String value=component.value();
            System.out.println(value);
        }

        //获取属性上面的注解
        //1.获取属性对象
        //获取Class对象
        Class clz1=Class.forName("factory.Apple");
        Field brand=clz1.getDeclaredField("brand");

        if (brand.isAnnotationPresent(Resource.class)){
            //获取注解对象
            Resource resource=(Resource)brand.getAnnotation(Resource.class);
            //调用注解对象里面方法
            String name=resource.name();
            System.out.println(name);
        }

        //获取方法上面的注解
        //获取Class对象
        Class clz2=Class.forName("factory.Apple");
        Method getBrand=clz2.getDeclaredMethod("getBrand");
        if (getBrand.isAnnotationPresent(RequestMapping.class)){
            //获取注解对象
            RequestMapping resource=(RequestMapping)getBrand.getAnnotation(RequestMapping.class);
            //调用注解对象里面方法
            String value=resource.value();
            String method=resource.method();
            System.out.println(value+","+method);
        }
    }
}
